//-----------------------------------------------------------------------
// <copyright file="a_UserTypeDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_UserTypeDAOBase
    {
        #region Common methods
        public virtual a_UserType Createa_UserTypeFromReader(IDataReader reader)
        {
            a_UserType item = new a_UserType();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UserTypeID"))) item.UserTypeID = (int)reader["UserTypeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("AccessLevel"))) item.AccessLevel = (byte)reader["AccessLevel"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_UserType methods
            
        public virtual void Createa_UserType(a_UserType a_UserType)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_UserTypes_ins");
                
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_UserType.Name);
                database.AddInParameter(dbCommand, "@AccessLevel", DbType.Byte, a_UserType.AccessLevel);
                database.AddOutParameter(dbCommand, "@UserTypeID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_UserType.UserTypeID = (int)database.GetParameterValue(dbCommand, "@UserTypeID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_UserType methods
        
        public virtual void Updatea_UserType(a_UserType a_UserType)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_UserTypes_upd");            
                
                database.AddInParameter(dbCommand, "@UserTypeID", DbType.Int32, a_UserType.UserTypeID);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_UserType.Name);
                database.AddInParameter(dbCommand, "@AccessLevel", DbType.Byte, a_UserType.AccessLevel);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_UserType methods
        public virtual void Deletea_UserType(int userTypeID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_UserTypes_del");
                
                database.AddInParameter(dbCommand, "@UserTypeID", DbType.Int32, userTypeID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_UserType methods
        
        public virtual a_UserType Geta_UserType(int userTypeID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_UserTypes_sel");
                
                database.AddInParameter(dbCommand, "@UserTypeID", DbType.Int32, userTypeID);
                
                a_UserType a_UserType =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_UserType = Createa_UserTypeFromReader(reader);
                        reader.Close();
                    }
                }
                return a_UserType;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_UserTypeList methods
        public virtual a_UserTypeCollection Geta_UserTypeList(a_UserTypeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_UserTypes_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_UserTypeCollection a_UserTypeCollection = new a_UserTypeCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_UserType a_UserType = Createa_UserTypeFromReader(reader);
                        a_UserTypeCollection.Add(a_UserType);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_UserTypeCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_UserTypeCollection Geta_UserTypeList(a_UserTypeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_UserTypeList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}